#include "custommessagebox.h"

#include <QPixmap>
#include <QStyle>
#include <QApplication>
#include <QMouseEvent>

CustomMessageBox::CustomMessageBox(QWidget *parent)
    : QDialog(parent), m_clickedButton(Ok)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setObjectName("customMessageBox");
    initUI();
}

CustomMessageBox::~CustomMessageBox()
{
}

void CustomMessageBox::initUI()
{
    setModal(true);
    setMinimumSize(300, 150);


    QWidget *wTitle = new QWidget(this);
    wTitle->setFixedHeight(24);
    wTitle->setObjectName("msgboxtitle");


    QWidget *wBody = new QWidget(this);
    wBody->setObjectName("msgboxBody");


    // 创建控件
    m_iconLabel = new QLabel(this);
    m_iconLabel->setFixedSize(18, 18);
    m_iconLabel->setAlignment(Qt::AlignCenter);


    QHBoxLayout *hTitle = new QHBoxLayout(wTitle);
    hTitle->setContentsMargins(0,0,0,0);
    hTitle->setSpacing(0);
    hTitle->addSpacing(12);
    hTitle->addWidget(m_iconLabel);
    hTitle->addSpacing(12);

    m_titleLabel = new QLabel(this);
    m_titleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_titleLabel->setWordWrap(true);
    hTitle->addWidget(m_titleLabel);

    m_textLabel = new QLabel();
    m_textLabel->setWordWrap(true);
    m_textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    m_buttonLayout = new QHBoxLayout();
    m_buttonLayout->addStretch();

    QVBoxLayout *vBody = new QVBoxLayout(wBody);
    vBody->addWidget(m_textLabel);
    vBody->addLayout(m_buttonLayout);


    QVBoxLayout *v = new QVBoxLayout(this);
    v->setSpacing(0);
    v->addWidget(wTitle);
    v->addWidget(wBody);
}

void CustomMessageBox::setText(const QString &text)
{
    m_textLabel->setText(text);
}

void CustomMessageBox::setIcon(Icon icon)
{


    m_iconLabel->setPixmap(getIconPixmap(icon));
}

void CustomMessageBox::setWindowTitle(const QString &title)
{
    m_titleLabel->setText(title);
}

QPixmap CustomMessageBox::getIconPixmap(Icon icon) const
{
    QStyle::StandardPixmap standardPixmap;
    //QPixmap standardPixmap;
    //standardPixmap = standardPixmap.scaled(m_iconLabel->width(), m_iconLabel->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    switch (icon) {
    case Information: standardPixmap = QStyle::SP_MessageBoxInformation; break;
    case Warning:     standardPixmap = QStyle::SP_MessageBoxWarning; break;
    case Critical:    standardPixmap = QStyle::SP_MessageBoxCritical; break;
    case Question:    standardPixmap = QStyle::SP_MessageBoxQuestion; break;
    default:          return QPixmap();
    }

    return style()->standardPixmap(standardPixmap).scaled(m_iconLabel->width(), m_iconLabel->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}

void CustomMessageBox::addButton(const QString &text, ButtonRole role)
{
    QPushButton *button = new QPushButton(text, this);
    m_buttonLayout->addWidget(button);
    m_buttonRoles[button] = role;

    connect(button, &QPushButton::clicked, this, &CustomMessageBox::onButtonClicked);
}

void CustomMessageBox::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (button && m_buttonRoles.contains(button)) {
        m_clickedButton = m_buttonRoles[button];
        accept();
    }
}

CustomMessageBox::ButtonRole CustomMessageBox::clickedButton() const
{
    return m_clickedButton;
}

void CustomMessageBox::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        m_StartPos = e->globalPos()- frameGeometry().topLeft();

    }

     e->accept();
}

void CustomMessageBox::mouseMoveEvent(QMouseEvent *e)
{
   if (e->buttons() & Qt::LeftButton) {
       this->move(e->globalPos() - m_StartPos);
    }

    e->accept();
}

void CustomMessageBox::mouseReleaseEvent(QMouseEvent *e)
{

}

// 静态方法实现
int CustomMessageBox::information(QWidget *parent, const QString &title,
                                const QString &text, ButtonRoles buttons,
                                ButtonRoles defaultButton)
{
    CustomMessageBox box(parent);
    box.setWindowTitle(title);
    box.setText(text);
    box.setIcon(Information);

    // 添加按钮
//    if (buttons & Ok) box.addButton(QObject::tr("OK"), Ok);
//    if (buttons & Yes) box.addButton(QObject::tr("Yes"), Yes);
//    if (buttons & No) box.addButton(QObject::tr("No"), No);
//    if (buttons & Cancel) box.addButton(QObject::tr("Cancel"), Cancel);
//    if (buttons & Close) box.addButton(QObject::tr("Close"), Close);

     if (buttons & Ok) box.addButton("确定", Ok);
     if (buttons & Yes) box.addButton("确定", Yes);
     if (buttons & No) box.addButton("取消", No);
     if (buttons & Cancel) box.addButton("取消", Cancel);
     if (buttons & Close) box.addButton("关闭", Close);

    // 设置默认按钮
    // 这里可以添加代码来设置默认按钮的高亮或焦点

    box.exec();
    return box.clickedButton();
}

int CustomMessageBox::warning(QWidget *parent, const QString &title,
                            const QString &text, ButtonRoles buttons,
                            ButtonRoles defaultButton)
{
    CustomMessageBox box(parent);
    box.setWindowTitle(title);
    box.setText(text);
    box.setIcon(Warning);

    // 添加按钮
    if (buttons & Ok) box.addButton("确定", Ok);
    if (buttons & Yes) box.addButton("确定", Yes);
    if (buttons & No) box.addButton("取消", No);
    if (buttons & Cancel) box.addButton("取消", Cancel);
    if (buttons & Close) box.addButton("关闭", Close);

    box.exec();
    return box.clickedButton();
}

int CustomMessageBox::critical(QWidget *parent, const QString &title,
                             const QString &text, ButtonRoles buttons,
                             ButtonRoles defaultButton)
{
    CustomMessageBox box(parent);
    box.setWindowTitle(title);
    box.setText(text);
    box.setIcon(Critical);

    // 添加按钮
    if (buttons & Ok) box.addButton("确定", Ok);
    if (buttons & Yes) box.addButton("确定", Yes);
    if (buttons & No) box.addButton("取消", No);
    if (buttons & Cancel) box.addButton("取消", Cancel);
    if (buttons & Close) box.addButton("关闭", Close);

    box.exec();
    return box.clickedButton();
}

CustomMessageBox::ButtonRole CustomMessageBox::question(QWidget *parent, const QString &title,
                             const QString &text, ButtonRoles buttons,
                             ButtonRoles defaultButton)
{
    CustomMessageBox box(parent);
    box.setWindowTitle(title);
    box.setText(text);
    box.setIcon(Question);

    // 添加按钮
    if (buttons & Ok) box.addButton("确定", Ok);
    if (buttons & Yes) box.addButton("确定", Yes);
    if (buttons & No) box.addButton("取消", No);
    if (buttons & Cancel) box.addButton("取消", Cancel);
    if (buttons & Close) box.addButton("关闭", Close);

    box.exec();
    return box.clickedButton();
}
